@inject Bit.Core.Services.II18nService i18nService
@model SsoConfigEditViewModel
@{
    ViewData["Title"] = i18nService.T("EditSsoConfig");
}

@section Scripts {
    <script type="text/javascript">
        function toggleVisibility() {
            var value = $('#Data_ConfigType').val();
            if (value == 'OpenIdConnect') {
                $('.oidc-config').show();
                $('.saml-config').hide();
            } else {
                $('.oidc-config').hide();
                $('.saml-config').show();
            }
        }

        $(function () {
            // Set initial visibility
            toggleVisibility();

            // Toggle visibiity on change
            $('#Data_ConfigType').change(function () {
                toggleVisibility();
            });

            $('.copy-button').on('click', function () {
                const $control = $(this).closest('.form-group').find('input[type="text"], textarea');
                if ($control.length > 0) {
                    const elem = $control[0];
                    elem.select();
                    elem.setSelectionRange(0, $control.val().length);
                    document.execCommand('copy');
                }
            });
        });
    </script>
}

<div class="page-header">
    <h1>@i18nService.T("SingleSignOn")</h1>
</div>

<form method="post" id="edit-form">
    <div class="form-group">
        <div class="form-check">
            <input asp-for="Enabled" type="checkbox" class="form-check-input">
            <label asp-for="Enabled" class="form-check-label"></label>
        </div>
    </div>

    <div class="row">
        <div class="col-7 form-group">
            <label asp-for="Data.ConfigType"></label>
            <select asp-for="Data.ConfigType" asp-items="Model.ConfigTypes" class="form-control"></select>
        </div>
    </div>

    <!-- OIDC -->
    <div class="oidc-config">
        <div class="config-section">
            <h2>@i18nService.T("OpenIdConnectConfig")</h2>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.CallbackPath">@i18nService.T("CallbackPath")</label>
                    <div class="input-group">
                        <input asp-for="Data.CallbackPath" class="form-control" readonly>
                        <div class="input-group-append">
                            <button type="button" class="btn btn-outline-secondary copy-button"
                                    aria-label="@i18nService.T("CopyCallbackPath")" title="@i18nService.T("CopyCallbackPath")"
                                    tabindex="-1">
                                <i class="fa fa-lg fa-clone" aria-hidden="true"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SignedOutCallbackPath">@i18nService.T("SignedOutCallbackPath")</label>
                    <div class="input-group">
                        <input asp-for="Data.SignedOutCallbackPath" class="form-control" readonly>
                        <div class="input-group-append">
                            <button type="button" class="btn btn-outline-secondary copy-button"
                                    aria-label="@i18nService.T("CopySignedOutCallbackPath")" title="@i18nService.T("CopySignedOutCallbackPath")"
                                    tabindex="-1">
                                <i class="fa fa-lg fa-clone" aria-hidden="true"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.Authority">@i18nService.T("Authority")</label>
                    <input asp-for="Data.Authority" class="form-control">
                    <span asp-validation-for="Data.Authority" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.ClientId">@i18nService.T("ClientId")</label>
                    <input asp-for="Data.ClientId" class="form-control">
                    <span asp-validation-for="Data.ClientId" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.ClientSecret">@i18nService.T("ClientSecret")</label>
                    <input asp-for="Data.ClientSecret" class="form-control">
                    <span asp-validation-for="Data.ClientSecret" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.MetadataAddress">@i18nService.T("MetadataAddress")</label>
                    <input asp-for="Data.MetadataAddress" class="form-control">
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <div class="form-check">
                        <input asp-for="Data.GetClaimsFromUserInfoEndpoint" type="checkbox" class="form-check-input">
                        <label asp-for="Data.GetClaimsFromUserInfoEndpoint" class="form-check-label"></label>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="saml-config">
        <!-- SAML2 SP -->
        <div class="config-section">
            <h2>@i18nService.T("SamlSpConfig")</h2>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SpEntityId">@i18nService.T("SpEntityId")</label>
                    <div class="input-group">
                        <input asp-for="Data.SpEntityId" class="form-control" readonly>
                        <div class="input-group-append">
                            <button type="button" class="btn btn-outline-secondary copy-button"
                                    aria-label="@i18nService.T("CopySpEntityId")" title="@i18nService.T("CopySpEntityId")"
                                    tabindex="-1">
                                <i class="fa fa-lg fa-clone" aria-hidden="true"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SpAcsUrl">@i18nService.T("SpAcsUrl")</label>
                    <div class="input-group">
                        <input asp-for="Data.SpAcsUrl" class="form-control" readonly>
                        <div class="input-group-append">
                            <button type="button" class="btn btn-outline-secondary copy-button"
                                    aria-label="@i18nService.T("CopySpAcsUrl")" title="@i18nService.T("CopySpAcsUrl")"
                                    tabindex="-1">
                                <i class="fa fa-lg fa-clone" aria-hidden="true"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="col-1">
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SpNameIdFormat">@i18nService.T("NameIdFormat")</label>
                    <select asp-for="Data.SpNameIdFormat" asp-items="Model.SpNameIdFormats"
                            class="form-control"></select>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SpOutboundSigningAlgorithm">@i18nService.T("OutboundSigningAlgorithm")</label>
                    <select asp-for="Data.SpOutboundSigningAlgorithm" asp-items="Model.SigningAlgorithms"
                            class="form-control"></select>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.SpSigningBehavior">@i18nService.T("SigningBehavior")</label>
                    <select asp-for="Data.SpSigningBehavior" asp-items="Model.SigningBehaviors"
                            class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <div class="form-check">
                    <input asp-for="Data.SpWantAssertionsSigned" type="checkbox" class="form-check-input">
                    <label asp-for="Data.SpWantAssertionsSigned" class="form-check-label"></label>
                </div>
            </div>
            <div class="form-group">
                <div class="form-check">
                    <input asp-for="Data.SpValidateCertificates" type="checkbox" class="form-check-input">
                    <label asp-for="Data.SpValidateCertificates" class="form-check-label"></label>
                </div>
            </div>
        </div>

        <!-- SAML2 IDP -->
        <div class="config-section">
            <h2>@i18nService.T("SamlIdpConfig")</h2>

            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpEntityId">@i18nService.T("EntityId")</label>
                    <input asp-for="Data.IdpEntityId" class="form-control">
                    <span asp-validation-for="Data.IdpEntityId" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpBindingType"></label>
                    <select asp-for="Data.IdpBindingType" asp-items="Model.BindingTypes" class="form-control"></select>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpSingleSignOnServiceUrl">@i18nService.T("SingleSignOnServiceUrl")</label>
                    <input asp-for="Data.IdpSingleSignOnServiceUrl" class="form-control">
                    <span asp-validation-for="Data.IdpSingleSignOnServiceUrl" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpSingleLogoutServiceUrl">@i18nService.T("SingleLogoutServiceUrl")</label>
                    <input asp-for="Data.IdpSingleLogoutServiceUrl" class="form-control">
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpArtifactResolutionServiceUrl"></label>
                    <input asp-for="Data.IdpArtifactResolutionServiceUrl" class="form-control">
                    <span asp-validation-for="Data.IdpArtifactResolutionServiceUrl" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpX509PublicCert">@i18nService.T("X509PublicCert")</label>
                    <textarea asp-for="Data.IdpX509PublicCert" class="form-control form-control-sm text-monospace" rows="6"></textarea>
                    <span asp-validation-for="Data.IdpX509PublicCert" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-7 form-group">
                    <label asp-for="Data.IdpOutboundSigningAlgorithm"></label>
                    <select asp-for="Data.IdpOutboundSigningAlgorithm" asp-items="Model.SigningAlgorithms"
                            class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <div class="form-check">
                    <input asp-for="Data.IdpAllowUnsolicitedAuthnResponse" type="checkbox" class="form-check-input">
                    <label asp-for="Data.IdpAllowUnsolicitedAuthnResponse" class="form-check-label"></label>
                </div>
            </div>
            <div class="form-group">
                <div class="form-check">
                    <input asp-for="Data.IdpDisableOutboundLogoutRequests" type="checkbox" class="form-check-input">
                    <label asp-for="Data.IdpDisableOutboundLogoutRequests" class="form-check-label"></label>
                </div>
            </div>
            <div class="form-group">
                <div class="form-check">
                    <input asp-for="Data.IdpWantAuthnRequestsSigned" type="checkbox" class="form-check-input">
                    <label asp-for="Data.IdpWantAuthnRequestsSigned" class="form-check-label"></label>
                </div>
            </div>
        </div>
    </div>
</form>

<div class="d-flex">
    <button type="submit" class="btn btn-primary" form="edit-form">@i18nService.T("Save")</button>
    <a class="btn btn-outline-secondary ml-1" asp-action="index">@i18nService.T("Cancel")</a>
</div>
